<?php 

$errors = array();

if(isset($_POST['action'])){
	
	$action = $_POST['action'];
	
	if('payment'==$action){
		//submit add fee
		$insertable = true;
		
		$room = $_POST['room'];
		$contract = $_POST['contract'];
		$rentalFee = $_POST['rentalFee'];
		$from = $_POST['from'];
		$to = $_POST['to'];
		$electricDegree = $_POST['electricDegree'];
		$electricFee = $_POST['electricFee'];
		
	
		
		if(trim($room) == ''){
			$insertable = false;
			$errors['room'] = '房间不能为空！';
		}
		
		if(trim($contract) == ''){
			$insertable = false;
			$errors['contract'] = '合同号不能为空';
		}
		
		if(trim($rentalFee) == ''){
			$insertable = false;
			$errors['rentalFee'] = '租金不能为空';
		}
		
		if(trim($from) == ''){
			$insertable = false;
			$errors['from'] = '开始时间不能为空';
		}
		
		if(trim($to) == ''){
			$insertable = false;
			$errors['to'] = ' 结束时间不能为空';
		}
		
		if(trim($rentalFee)!= '' && !is_numeric($rentalFee)){
			$insertable = false;
			$errors['rentalFee'] = '租金金额不对，只能输入数字';
		}
		
		if(trim($electricDegree)!= '' && !is_numeric($electricDegree)){
			$insertable = false;
			$errors['electricDegree'] = '电表度数不对，只能输入数字';
		}
		
		if(trim($electricFee)!= '' && !is_numeric($electricFee)){
			$insertable = false;
			$errors['electricFee'] = '电费金额不对，只能输入数字';
		}
		
		if(trim($from) != '' &&($from = strtotime($from))=== false){
			//echo "<span class='error'>入住日期格式不对，正确的格式 年-月-日</span></br>";
			$errors['from'] = "租金开始时间格式不对，正确的格式 年-月-日";
			$insertable = false;
		}else{
			$from = date('Y-m-d H:i:s', $from);
		}
		
		if(trim($to) != '' &&($to = strtotime($to))=== false){
			//echo "<span class='error'>入住日期格式不对，正确的格式 年-月-日</span></br>";
			$errors['to'] = "租金结束时间格式不对，正确的格式 年-月-日";
			$insertable = false;
		}else{
			$to = date('Y-m-d H:i:s', $to);
		}
		
		if($insertable){
			$result = mysql_query("insert into  `journal` (`room_id`,`contract_id`,`rentalfee`,`from`,`to`,`electric_degree`,`electric_fee`,`create_dt`)" 
			." VALUES ('$room','$contract','$rentalFee','$from','$to','$electricDegree','$electricFee','".date('Y-m-d H:i:s')."')");
			
			if(!$result){
				die(mysql_error());
			}else{
				echo"<div class='rentSuccess'>房间 $room $from 到 $to 交租成功!</div>";
			}
			
		}
		
	}else if('checkin' == $action){
	
		$insertable = true;
		
		$room = $_POST["room"];
		$renter = $_POST['renter'];
		$idNo = $_POST['idno'];
		$rental = $_POST['rental'];
		$checkinDateStr = $_POST['checkinDate'];
		$electricMeter = $_POST['electricMeter'];
		
		//requese validate
		if(trim($room) == ''){
			$insertable = false;
			//echo "<span class='error'>需要选择房间！</span></br>";
			$errors['room'] = "<span class='error'>需要选择房间！</span>";
		}
		
		if(trim($renter) == ''){
			$insertable = false;
			//echo "<span class='error'>需要填写租房者！</span></br>";
			$errors['renter'] =  "<span class='error'>需要填写租房者！</span>";
		}
		
		if(trim($rental) == ''){
			$insertable = false;
			//echo "<span class='error'>需要填写租房金额！</span></br>";
			$errors['rental'] = "<span class='error'>需要填写租房金额！</span>";
		}
		
		if(trim($checkinDateStr) == ''){
			$insertable = false;
			//echo "<span class='checkinDate'>需要填写入住时间！</span></br>";
			$errors['checkinDate'] = "<span class='error'>需要填写入住时间！</span>";
		}
		
		if(trim($electricMeter) == ''){
				$insertable = false;
				//echo "<span class='error'>需要填写电表度数！</span></br>";
				$errors['electricMeter'] = "<span class='error'>需要填写电表度数！</span>";
			}
		
		//value validate
		$checkinDate = null;
		
		if(trim($checkinDateStr) != '' &&($checkinDate = strtotime($checkinDateStr))=== false){
			//echo "<span class='error'>入住日期格式不对，正确的格式 年-月-日</span></br>";
			$errors['checkinDate'] = "<span class='error'>入住日期格式不对，正确的格式 年-月-日</span>";
			$insertable = false;
		}else{
			$checkinDate = date('Y-m-d H:i:s', $checkinDate);
		}
		
		if(trim($rental) != '' && !is_numeric($rental)){
			$insertable = false;
			//echo "<span class='error'>租金金额不对，只能输入数字！</span></br>";
			$errors['rental'] = "<span class='error'>租金金额不对，只能输入数字！</span>";
		}
		
		if(trim($electricMeter) != '' &&!is_numeric($electricMeter)){
			$insertable = false;
			//echo "<span class='error'>电表度数不对，只能输入数字！</span></br>";
			$errors['electricMeter'] = "<span class='error'>电表度数不对，只能输入数字！</span>";
		}
		
		//update db
		if($insertable){
		
			//echo "room id: [$room] renter: [$renter]  idNo: [$idNo] rental: [$rental] checkinDate: [$checkinDate] electricMeter  [$electricMeter]";
		
			mysql_query("START TRANSACTION");
			
			$result = mysql_query("insert into `rent`.`contract` (`room` ,`renter` ,`rental` ,`ele_meter_start` ,`idNo`,`check_in`,`create_dt`)"
			."VALUES ( '$room','$renter' ,'$rental','$electricMeter', '$idNo','$checkinDate','".date('Y-m-d H:i:s')."');");	
		
			if(!$result){
				die("error when insert the constract record: ".mysql_error());
			}
			
			$result = mysql_query("update room set status = 'rented' where id ='".$room."'");
			
			//insert a initial journal for this contract
			$result = mysql_query("insert into  `journal` (`room_id`,`contract_id`,`rentalfee`,`from`,`to`,`electric_degree`,`electric_fee`,`create_dt`)" 
			." VALUES ('$room',(select id from `contract` where `room`=".$room." order by id desc limit 0,1),'0','$checkinDate','$checkinDate','$electricMeter','0','".date('Y-m-d H:i:s')."')");
			
			mysql_query("COMMIT");
			
			if(!$result){
				die("error when update room status: ".mysql_error());
			}else{
				echo"<div class='rentSuccess'>房间 $room 出租成功!</div>";
			}
			
		}
	}else if('checkout' == $action){
		
		$insertable = true;
		$room = $_POST['room'];
		$contract = $_POST['contract'];
		$checkoutDate = $_POST['checkoutDate'];
		$electricDegree = $_POST['electricDegree'];
		$electricFee = $_POST['electricFee'];
		$balancedAmount = $_POST['balancedAmount'];
		
		if(trim($electricDegree) == ''){
			$insertable = false;
			$errors['electricDegree'] = "需要填写电表度数！";
		}
				
		if(trim($electricDegree) != '' &&!is_numeric($electricDegree)){
			$insertable = false;
			$errors['electricDegree'] = "电表度数不对，只能输入数字！";
		}
		
		if(trim($electricFee) == ''){
			$insertable = false;
			$errors['electricFee'] = "需要填写电费！";
		}
				
		if(trim($electricFee) != '' &&!is_numeric($electricFee)){
			$insertable = false;
			$errors['electricFee'] = "电费不对，只能输入数字！";
		}
		
		if($insertable){
		
			mysql_query('START TRANSACTION') or exit(mysql_error());
			
			$result = mysql_query("update room set status = 'available' where id =".$room);
			if($result){
				//update checkout date and end electric degree
				$result2 = mysql_query("update `contract` set `check_out`='".$checkoutDate."',ele_meter_end=".$electricDegree."  where room=".$room." and id=".$contract."");
				if($result2){
				
					//Set balanced amount when checkout
					$result3 = mysql_query("insert into  `journal` (`room_id`,`contract_id`,`rentalfee`,`from`,`to`,`electric_degree`,`electric_fee`,`create_dt`)" 
					." VALUES ('$room','$contract','$balancedAmount','$checkoutDate','$checkoutDate','$electricDegree','$electricFee','".date('Y-m-d H:i:s')."')");
				
					if($result3){
						echo "<div class='rentSuccess'>房间 $room 退租成功!</div>";
					}else{
						die(mysql_error());
					}
				}
			}else{
				die(mysql_error());
			}
			
				mysql_query('COMMIT') or exit(mysql_error());
			}
	}
}

?>